﻿@page "/Departments/DepartmentView"
@inherits _BaseView

<MaanfeeDashboardPage>
    <MaanfeeDashboardPageHeader PageTitle="@DashboardResource.StringDepartment" />

    @if (!IsLoaded)
    {
        <PublicOverlay />
    }
    else
    {
        <MudCard>
            <MudCardHeader>
                <MudText Typo="Typo.button" Color="Color.Primary">
                    @DashboardResource.StringDepartment
                </MudText>
            </MudCardHeader>
            <MudDivider DividerType="DividerType.FullWidth" />
            <MudCardContent>
                <MudGrid>
                    <MudItem md="6" xs="6">
                        <EditForm OnValidSubmit="OnSubmit" Model="SubmitDepartmentViewModel">
                            <DataAnnotationsValidator />
                            <MudCard>
                                <MudCardContent>
                                    <MudGrid>
                                        <MudItem md="12" xs="12">
                                            <MudTextField Variant="Variant.Outlined" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.AccountTree"
                                                      @bind-Value="SubmitDepartmentViewModel.Title"
                                                      For="@(() => SubmitDepartmentViewModel.Title)"
                                                      Label="@DashboardResource.StringDepartment" />
                                        </MudItem>
                                        <MudItem md="12" xs="12">
                                            <MudAutocomplete T="DropDownDepartmentViewModel" Label="@DashboardResource.StringParent"
                                                         ResetValueOnEmptyText="true" Clearable="true" MaxItems="null"
                                                         @bind-Value="SubmitDepartmentViewModel.Parent" Variant="Variant.Outlined"
                                                         SearchFunc="GetParentsAsync" ToStringFunc="@(e=> e==null?null : $"{e.Title}")" />
                                        </MudItem>
                                    </MudGrid>
                                </MudCardContent>
                            </MudCard>

                            <MudPaper Class="pa-3 my-2 ActionColor" Elevation="3" Outlined="true" Square="true">
                                <MudStack Row="true" Spacing="2" AlignItems="AlignItems.Start">
                                    @if (CanCreate)
                                    {
                                        <MudButton Disabled="@IsProcessing" ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Tertiary">
                                            @if (IsProcessing)
                                            {
                                                <MudProgressCircular Size="Size.Small" Indeterminate="true" />
                                                <MudText Class="ms-2">@DashboardResource.StringProcessing</MudText>
                                            }
                                            else
                                            {
                                                <MudIcon Icon="@Icons.Material.Filled.Save" Color="Color.Surface" />
                                                <MudText>@DashboardResource.StringSave</MudText>
                                            }
                                        </MudButton>
                                    }
                                    @if (CanDelete)
                                    {
                                        <MudButton OnClick="@((e) => OpenDeleteDialog())" Variant="Variant.Filled" Color="Color.Error"
                                           Disabled=@(SubmitDepartmentViewModel?.Id == 0 ||SubmitDepartmentViewModel?.Id == null ? true : false)>
                                            <MudIcon Icon="@Icons.Material.Filled.DeleteForever" Color="Color.Surface" />
                                            <MudText>@DashboardResource.StringDelete</MudText>
                                        </MudButton>
                                    }
                                    <MudButton OnClick="@ResetAsync" Variant="Variant.Filled" Color="Color.Secondary" 
                                        StartIcon="@Icons.Material.Filled.Refresh" IconColor="Color.Surface">
                                        @DashboardResource.StringReload
                                    </MudButton>
                                </MudStack>
                            </MudPaper>

                            @{
                                if (SelectedValue != null)
                                {
                                    SubmitDepartmentViewModel.Id = SelectedValue?.Id ?? null;
                                    SubmitDepartmentViewModel.Title = SelectedValue?.Title;
                                    SubmitDepartmentViewModel.Parent = Parents.FirstOrDefault(x => x.Id == SelectedValue?.IdParent) ?? null;

                                    SelectedValue = null;
                                }
                            }
                        </EditForm>
                    </MudItem>
                    <MudItem md="6" xs="6">
                        <MudCard>
                            <MudCardHeader>
                                <MudText Color="Color.Error">@DashboardResource.StringOrganizationalChart</MudText>
                            </MudCardHeader>
                            <MudCardContent>
                                @*@bind-SelectedValues="SelectedValues" @bind-ActivatedValue="ActivatedValue" *@
                                <MudTreeViewOld Items="Departments" Hover="true" Color="Color.Tertiary" @bind-SelectedValue="SelectedValue">
                                    <ItemTemplate>
                                        <MudTreeViewItemOld Items="@context.Department1" Expanded="true"
                                                     Value="@context" Text="@context.Title" EndTextTypo="@Typo.caption" />
                                    </ItemTemplate>
                                </MudTreeViewOld>
                            </MudCardContent>
                        </MudCard>
                    </MudItem>
                </MudGrid>
            </MudCardContent>
        </MudCard>

        @*
    <div style="width: 100%">
    <MudText Typo="@Typo.subtitle1">Activated Key: @(ActivatedValue?.Id)

    </MudText>
    <MudText Typo="@Typo.subtitle1">Activated item: @(ActivatedValue?.Title)</MudText>
    </div>

    <MudTreeView Items="TreeItems" CanHover="true" ExpandOnClick="true" Color="Color.Tertiary"
    @bind-ActivatedValue="ActivatedValue" @bind-SelectedValues="SelectedValues">
    <ItemTemplate>
    <MudTreeViewItem @bind-Expanded="@context.IsExpanded" Items="@context.TreeItems"
    Value="@context" Text="@context.Title" EndTextTypo="@Typo.caption" />
    </ItemTemplate>
    </MudTreeView>
    *@

    }

</MaanfeeDashboardPage>

